package com.ruoyi.manage.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.manage.domain.FormSettingSmall;
import com.ruoyi.manage.mapper.FormSettingBigMapper;
import com.ruoyi.manage.domain.FormSettingBig;
import com.ruoyi.manage.service.IFormSettingBigService;

/**
 * 清单大项Service业务层处理
 *
 * @author zww
 * @date 2025-07-14
 */
@Service
public class FormSettingBigServiceImpl implements IFormSettingBigService {
    @Autowired
    private FormSettingBigMapper formSettingBigMapper;

    /**
     * 查询清单大项
     *
     * @param formSettingBigId 清单大项主键
     * @return 清单大项
     */
    @Override
    public FormSettingBig selectFormSettingBigByFormSettingBigId(Long formSettingBigId) {
        return formSettingBigMapper.selectFormSettingBigByFormSettingBigId(formSettingBigId);
    }

    public List<FormSettingBig> selectFormSettingBigByFormSettingId(Long formSettingId) {
        List<FormSettingBig> formSettingBigList = formSettingBigMapper.selectFormSettingBigByFormSettingId(formSettingId);
        for (FormSettingBig formSettingBigItem : formSettingBigList) {
            formSettingBigItem.setFormSettingSmallList(formSettingBigMapper.selectFormSettingSmallList(formSettingBigItem.getFormSettingBigId()));
        }
      return  formSettingBigList ;
    }


    /**
     * 查询清单大项列表
     *
     * @param formSettingBig 清单大项
     * @return 清单大项
     */
    @Override
    public List<FormSettingBig> selectFormSettingBigList(FormSettingBig formSettingBig) {
        List<FormSettingBig> formSettingBigList = formSettingBigMapper.selectFormSettingBigList(formSettingBig);
        for (FormSettingBig formSettingBigItem : formSettingBigList) {
            formSettingBigItem.setFormSettingSmallList(formSettingBigMapper.selectFormSettingSmallList(formSettingBigItem.getFormSettingBigId()));
        }
        return formSettingBigList;
    }

    /**
     * 新增清单大项
     *
     * @param formSettingBig 清单大项
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFormSettingBig(FormSettingBig formSettingBig) {
        return  formSettingBigMapper.insertFormSettingBig(formSettingBig);
    }

    /**
     * 修改清单大项
     *
     * @param formSettingBig 清单大项
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFormSettingBig(FormSettingBig formSettingBig) {
        return formSettingBigMapper.updateFormSettingBig(formSettingBig);
    }

    /**
     * 批量删除清单大项
     *
     * @param formSettingBigIds 需要删除的清单大项主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFormSettingBigByFormSettingBigIds(Long[] formSettingBigIds) {
        formSettingBigMapper.deleteFormSettingSmallByFormSettingBigIds(formSettingBigIds);
        return formSettingBigMapper.deleteFormSettingBigByFormSettingBigIds(formSettingBigIds);
    }

    /**
     * 删除清单大项信息
     *
     * @param formSettingBigId 清单大项主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFormSettingBigByFormSettingBigId(Long formSettingBigId) {
        formSettingBigMapper.deleteFormSettingSmallByFormSettingBigId(formSettingBigId);
        return formSettingBigMapper.deleteFormSettingBigByFormSettingBigId(formSettingBigId);
    }

    @Override
    public List<FormSettingBig> selectFormSettingBigWithValue(Long formSettingId, Long surgeryFormId) {
        List<FormSettingBig> formSettingBigList = formSettingBigMapper.selectFormSettingBigByFormSettingId(formSettingId);
        for (FormSettingBig formSettingBigItem : formSettingBigList) {
            formSettingBigItem.setFormSettingSmallList(formSettingBigMapper.selectFormSettingSmallWithValue(formSettingBigItem.getFormSettingBigId(),surgeryFormId));
        }
        return formSettingBigList;
    }

    /**
     * 新增单小项信息
     *
     * @param formSettingBig 清单大项对象
     */
    public void insertFormSettingSmall(FormSettingBig formSettingBig) {
        List<FormSettingSmall> formSettingSmallList = formSettingBig.getFormSettingSmallList();
        Long formSettingBigId = formSettingBig.getFormSettingBigId();
        if (StringUtils.isNotNull(formSettingSmallList)) {
            List<FormSettingSmall> list = new ArrayList<FormSettingSmall>();
            for (FormSettingSmall formSettingSmall : formSettingSmallList) {
                formSettingSmall.setFormSettingBigId(formSettingBigId);
                list.add(formSettingSmall);
            }
            if (list.size() > 0) {
                formSettingBigMapper.batchFormSettingSmall(list);
            }
        }
    }
}
